package cn.class2.demo4

/**
  * Created by lzd on 2018/11/12.
  * object修饰就是单利对象，不需要new
  * object修饰的对象是静态的，所有里边的方法，变量也是静态的
  */


object Demo4 {

  val url = "www.baidu.com"

  def show(): Unit = {
    println(Demo4.url)
  }

  def main(args: Array[String]): Unit = {

    val d1 = Demo4
    val d2 = Demo4
    println(d1)
    println(d2)

    show
  }
}

/**
  * 不用实现main函数，就可以打印，app里有实现
  */
object Demo4_1 extends App {
  println("hello")
}

/**
  * apply的神奇
  * Array(1,2,3) List(1,2,3) 都是apply的功能
  */
object Demo4_2 {
  def apply(name: String, age: Int): Unit = {
    println(f"名字=${name},年龄=${age}")
  }

  def apply(a: Int, b: Int*): Int = {
    var res = 0
    for (index <- b){
      res+=index
    }
    res+a
  }

  def main(args: Array[String]): Unit = {
    val a = Demo4_2
    println(a)
    val b = Demo4_2("scala",12)
    println(b)
    val c = Demo4_2(2,2,2,2,2,2)
    println(c)
  }
}